<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Grom Hellscream
 * Date: 11/7/12
 * Time: 11:02 AM
 * To change this template use File | Settings | File Templates.
 */
// no direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.modellist');

class VastModelCongtrinhs extends JModelList
{
    /**
     * Constructor.
     *
     * @param	array	An optional associative array of configuration settings.
     * @see		JController
     * @since	1.6
     */
    public function __construct($config = array())
    {
        if (empty($config['filter_fields'])) {
            $config['filter_fields'] = array(
                'id', 'a.id',
                'ten', 'a.ten',
            );
        }

        parent::__construct($config);
    }

    /**
     * Method to auto-populate the model state.
     *
     * Note. Calling getState in this method will result in recursion.
     *
     * @return	void
     * @since	1.6
     */
    protected function populateState($ordering = null, $direction = null)
    {
        // List state information.
        //Load the filter state
        $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
        $this->setState('filter.search', $search);

        $state = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_state', '', 'string');
        $this->setState('filter.state', $state);

        //Load the parameters.
        $params = JComponentHelper::getParams('com_vast');
        $this->setState('params', $params);

        parent::populateState('a.ten', 'asc');
    }

    /**
     * Build an SQL query to load the list data.
     *
     * @return	JDatabaseQuery
     * @since	1.6
     */
    protected function getListQuery()
    {
        // Create a new query object.
        $db		= $this->getDbo();
        $query	= $db->getQuery(true);

        $query->select('a.*, h.*');
        $query->from('#__congtrinh AS a');
        $query->innerJoin('#__hosocanbo AS h ON h.id = a.hosocanbo_id');

        //Filter
        $search = $this->state->get('filter.search');
        if (!empty($search))
        {
            $query->where('(a.ten LIKE \'%' . $search . '%\')');
        }

        // Add the list ordering clause.
        $orderCol	= $this->state->get('list.ordering', 'a.ten');
        $orderDirn	= $this->state->get('list.direction', 'asc');
        $query->order($db->escape($orderCol.' '.$orderDirn));

        // echo nl2br(str_replace('#__','jos_',$query));
        return $query;
    }
}